import pyautogui as pg
import pathlib
import os,sys
import time
import pandas
root=pathlib.Path(os.path.dirname(os.path.realpath(sys.argv[0])))


def fp(filename):
    r_location = pg.locateOnScreen(str(root/filename), confidence=0.8, grayscale=True)

    if(r_location!=None):
        mouse_x=r_location[0]+8
        mouse_y=r_location[1]+10
        pg.moveTo(mouse_x,mouse_y)
        return True
    else:
        return False
def getname():
    df=pandas.read_clipboard()
    df=df.columns[0]
    df=df.split("/")
    df=df[-1].replace(".html",'')
    print(df)
    return df
def wp(filename):
    while not fp(filename) :
        time.sleep(1)
        print("wait %s"%(filename))    
def save(i=0):
    wp("02.png") 
    pg.moveRel(150,5)
    pg.click()
    pg.hotkey('ctrlleft','a')
    pg.hotkey('ctrlleft','c')
    df=pandas.read_clipboard()
    print(df.columns[0])
    pg.moveTo(200,400)
    pg.click(button='right')
    fp("01.png")
    pg.click()
    
    wp("03.png")
    time.sleep(1)
    pg.write(getname())
    wp("03.png")
    print(fp("03.png"))
    pg.moveRel(50,15)
    pg.click()

while True :
    save()
    time.sleep(2)
    if(fp("04.png")):
        pg.moveRel(50,15)
        pg.click()
        time.sleep(2)

    
    pg.moveTo(200,400)
    pg.click()
    time.sleep(2)

